<?php

namespace App\Models;


class BarsModel
{
    public static function barsClassList($conn,$BarsExample){
        $typeSql="SELECT `id`, `name`, `phone`,`addr`, `city`, `head_img`,`time`,`lng`,`lat`,`status` FROM `bars` where `id` = $BarsExample->id";
        return $conn->readConn($typeSql);
    }
    public static function barsClassListInLngOrLat($conn,$BarsExample){
        $typeSql="SELECT `id`, `name`, `phone`,`addr`, `city`, `head_img`,`time`,`lng`,`lat`,st_distance_sphere(point(lng,lat),
	point($BarsExample->lng,$BarsExample->lat)) AS juli FROM `bars` where `status` = 1  order by `juli` asc";
        return $conn->readConn($typeSql,true);
    }
    /**
     * 添加店铺
     * entry BarsExample
     * return int
     * */
    public static function addBars($conn,$BarsExample){
        $fundsSql ="INSERT INTO `bars`(`name`, `phone`,`addr`, `head_img`,`time`,`lng`,`lat`,`collectioncode`,`status`) VALUES ('$BarsExample->name','$BarsExample->phone','$BarsExample->addr','$BarsExample->head_img',NOW(),$BarsExample->lng,$BarsExample->lat,'$BarsExample->collectioncode',1)";
        return $conn->executeConn($fundsSql);
    }
    /**
     * 修改店铺可添加字段
     * entry BannerExample
     * return int
     * */
    public static function upBarsInside($conn,$BarsExample){
        $joinSql="";
        if($BarsExample->phone !=null){
            $joinSql.=" `phone` = '$BarsExample->phone',";
        }
        if($BarsExample->name !=null){
            $joinSql.=" `name` = '$BarsExample->name',";
        }
        if($BarsExample->addr !=null){
            $joinSql.=" `addr` = '$BarsExample->addr',";
        }
        if($BarsExample->head_img !=null){
            $joinSql.=" `head_img` = '$BarsExample->head_img',";
        }
        if($BarsExample->lng !=null){
            $joinSql.=" `lng` = '$BarsExample->lng',";
        }
        if($BarsExample->lat !=null){
            $joinSql.=" `lat` = $BarsExample->lat,";
        }
        if($BarsExample->status !=null){
            $joinSql.=" `status` = $BarsExample->status,";
        }
        if($BarsExample->collectioncode !=null){
            $joinSql.=" `collectioncode` = '$BarsExample->collectioncode',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `bars` SET $innerJoin WHERE `id` = '$BarsExample->id'";
            return $conn->executeConn($upUserSql);
        }
    }
}